//
//  CXLProgressImageView.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/27.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

/// 带下载进度UIImageView
class CXLProgressImageView: UIImageView {

    /// 外部传入的下载进度
    var progress:CGFloat = 0{
        
        didSet{
            progressView.progress = progress
            
        }
    }
    

    //MARK: -构造函数
    init(){
        super.init(frame: CGRectZero)
        
        addSubview(progressView)
        
        progressView.backgroundColor = UIColor(white: 1.0, alpha: 0.3)
        
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- 懒加载
    /// 下载进度视图
    private lazy var progressView:CXLProgressView = CXLProgressView()
    
}


class CXLProgressView:UIView {
    
    /// 内部的下载进度
    var progress:CGFloat = 0 {
        
        didSet{
            
            setNeedsDisplay()
        }
    }
    
    ///  绘制下载进度
    override func drawRect(rect: CGRect) {
        
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        
        let radius = min(rect.width, rect.height) * 0.5
        
        let startA = CGFloat(-M_PI_2)
        
        let endA = progress * CGFloat(M_PI * 2) + startA
        
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startA, endAngle: endA, clockwise: true)
        
        path.addLineToPoint(center)
        
        UIColor(white: 1.0, alpha: 0.5).setFill()
        
        path.fill()
    }
}